package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.LoginThreadLocal;
import com.project.entity.*;
import com.project.mapper.IApplyInfoMapper;
import com.project.mapper.IBiddMapper;
import com.project.mapper.IProcureApplyMapper;
import com.project.service.IBiddService;
import com.project.service.IProcureApplyService;
import com.project.util.SnowIdUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ProcureApplyServiceImpl implements IProcureApplyService {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private IProcureApplyMapper procureApplyMapper;

    @Autowired
    private IApplyInfoMapper applyInfoMapper;

    @Autowired
    private IBiddService biddService;

    /**
     * 查找招标书状态为已发布招标
     */
    @Override
    public IPage<ProcureApplyEntity> findByItem(int pageNO, int pageSize, int status) {
        QueryWrapper<ProcureApplyEntity> qw = new QueryWrapper<>();
        qw.eq("a_status", status);
        return procureApplyMapper.selectPage(new Page<>(pageNO, pageSize), qw);
    }

    /**
     * 采购对象和包含的商品对象
     */
    @Override
    public ProcureApplyEntity findById(String applyRef) {
        QueryWrapper<ProcureApplyEntity> qw = new QueryWrapper<>();
        qw.eq("a_applyRef", applyRef);
        ProcureApplyEntity procureApplyEntity = procureApplyMapper.selectOne(qw);

        QueryWrapper<ApplyInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a_applyRef", applyRef);
        List<ApplyInfoEntity> applyInfoEntityList = applyInfoMapper.selectList(queryWrapper);

        procureApplyEntity.setApplyInfoEntityList(applyInfoEntityList);
        return procureApplyEntity;

    }

    /**
     * 采购对象和包含的商品对象
     */
    @Override
    public void updateStatus(String applyRef, int status, String cause, String verifyName) {
        QueryWrapper<ProcureApplyEntity> qw = new QueryWrapper<>();
        qw.eq("a_applyRef", applyRef);
        ProcureApplyEntity procureApplyEntity = procureApplyMapper.selectOne(qw);


        procureApplyEntity.setStatus(status);
        procureApplyEntity.setVerifyTime(LocalDateTime.now());
        procureApplyEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        if (status == 2) {
            procureApplyEntity.setCause(cause);
        }
        procureApplyMapper.updateById(procureApplyEntity);
        String jsonObject =  JSONObject.toJSONString(procureApplyEntity);
        rabbitTemplate.convertAndSend("topicExchangeTwo","rabbit.ProcureApplySupplierVerifyResult",jsonObject);

        //Java对象转化为JSON对象，如果审核通过，就返回给后端
        //------------------------------------------------------------发送到mq

        if (status == 1) {
            QueryWrapper<ApplyInfoEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("a_applyRef", applyRef);
            List<ApplyInfoEntity> applyInfoEntityList = applyInfoMapper.selectList(queryWrapper);
            StringBuilder stringBuilder = new StringBuilder();
            for (ApplyInfoEntity a : applyInfoEntityList) {
                stringBuilder.append(a.getProductName() + " " + a.getColor() + " " + a.getSize() + " " + a.getApplyNumber() + "<br> ");
            }


            //生成招标书 添加到数据库
            BiddEntity biddEntity = new BiddEntity();
            biddEntity.setInviteId(SnowIdUtils.uniqueLongHex());
            biddEntity.setInfo(stringBuilder.toString());
            biddEntity.setApplyRef(applyRef);
            biddService.addBidd(biddEntity);


        }

    }




}
